#include <stdio.h>
#include "nuclei_sdk_hal.h"
#include "nuclei_libopt.h"
#include "anl_printf.h"

#define ARRAY_SIZE  10
uint8_t send_data[ARRAY_SIZE]={0,1,2,3,4,5,6,7,8,9};
uint8_t receive_data[ARRAY_SIZE]={0};
uint8_t send_n = 0;

void i2c_handler(void)
{
    send_n ++;
     I2C->CR_SR = 0x1;
    anl_printf("one byte send \r\n");
}



int main()
{

    i2c_init(I2C,0x3,0x3c,0x1);   //trise   fltr    n = (div +1 )*5 + trise      F(i2c) = F(core)/n
    i2c_enable(I2C);

     __enable_irq();
	ECLIC_Register_IRQ(I2C_IRQn, ECLIC_NON_VECTOR_INTERRUPT,
                                    ECLIC_LEVEL_TRIGGER, 1, 1,
                                    i2c_handler);	
    i2c_enable_int(I2C);
    while(i2c_flag_get(I2C,BUSY));

    i2c_mode_addr_config(I2C,0x10,I2C_WRITE);   //1
    i2c_send_start_write_cmd(I2C);
    while(i2c_flag_get(I2C,TIP)); 
    i2c_data_transmit(I2C,0x0);                 //2        

    i2c_data_transmit(I2C,0x5a);               //3

    i2c_data_transmit(I2C,0xa5);                //4

    i2c_data_transmit(I2C,0x55);                 //5

    i2c_data_transmit(I2C,0xaa);                //6
    

    i2c_data_write(I2C,0x11);                 //7

    i2c_close_transmit(I2C);

    while(i2c_flag_get(I2C,TIP));
    if(send_n == 7)
    {
        #ifdef CFG_SIMULATION
        pass_fail_simulation(1);
        #endif 
        anl_printf("I2C int ### PASS ###\r\n");
    }else{
        #ifdef CFG_SIMULATION
        pass_fail_simulation(0);
        #endif 
        anl_printf("I2C int ### FAIL ### send_n :%d\r\n",send_n);
    }



}
